const { Router } = require("express");
const addwarehouses = require("../model/Addwarehouse");

const router = new Router();
router.use(require("../middleware/auth.js"));
//添加新仓库的接口
// http://localhost:8000/api/addwarehouse/add
router.post("/add", (req, res) => {
  const { name, city, region, address, area } = req.body;
  try {
    const data = addwarehouses.create({ name, city, region, address, area });
    res.send({
      code: 200,
      status: true,
      msg: data,
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      status: false,
      msg: "添加失败",
    });
  }
});

//获取所有仓库的信息
//  http://localhost:8000/api/addwarehouse/getAll
router.get("/getAll", async (req, res) => {
  try {
    const data = await addwarehouses.find();
    res.send({
      code: 200,
      status: true,
      msg: data,
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      status: false,
      msg: "请求出错",
    });
  }
});

module.exports = router;
